package com.byzk.p2p.front.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.borrow.util.BorrowStatus;

/**
 * 前台工具类
 * 
 * @author LiuJY
 *
 */
public class FrontUtil {
	/**
	 * 格式化数字
	 * @param number 数字
	 * @return
	 */
	public static String numberFormat(Double number) {
//		#表示没有则为空，0表示如果没有则该位补0.
		DecimalFormat df = new DecimalFormat("#0.00");
		return df.format(number);
	}

	public static String getBorrowStatus(String status){
		if (StringUtils.isBlank(status)) {
			return "";
		}
		if (status.equals(BorrowStatus.NEW) || status.equals(BorrowStatus.BORROW)) {
			return "投资中";
		}else if (status.equals(BorrowStatus.CHECK)) {
			return "复审中";
		}else if (status.equals(BorrowStatus.PAY)) {
			return "还款中";
		}else if (status.equals(BorrowStatus.COMPLETE)) {
			return "已还完";
		}
		return "";
	}
	
	/**
	 * 组装公共分页HTML
	 * @param pageInfo 分页
	 * @param index 当前页
	 * @return
	 */
	public static StringBuilder pageHtml(PageInfo pageInfo,int index) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("<div class=\"pagination text-center\" id=\"pageDiv\">");
		if (pageInfo != null && CollectionUtils.isNotEmpty(pageInfo.getPageList())) {
			stringBuilder.append("<input type='hidden' id='index' value=").append(index==pageInfo.getMax()?pageInfo.getMax():index<=0?1:index).append(" />");
			stringBuilder.append("<input type='hidden' id='pageCounts' value=").append(pageInfo.getPages()).append(" />");
			stringBuilder.append("<ul class='page-nag'>");
			stringBuilder.append("<li><a href='javascript:doPage(").append(1).append(")'").append(" id='first'>").append("首页</a></li>");
			stringBuilder.append("<li><a href='javascript:doPage(").append(pageInfo.getIndex()-1).append(")'").append(" id='previous'>").append("上一页</a></li>");
			if (pageInfo.getPages() >1) {
				for (int i = pageInfo.getMin(); i <= pageInfo.getMax(); i++) {
					stringBuilder.append("<li>");
					if(i==pageInfo.getIndex()){
						stringBuilder.append("<a href='javascript:doPage(").append(i).append(")'").append(" class='active'>").append(i).append("</a>");
					}else{
						stringBuilder.append("<a href='javascript:doPage(").append(i).append(")'>").append(i).append("</a>");
					}
					stringBuilder.append("</li>");
				}
			}else if (pageInfo.getPages() == 1) {
				stringBuilder.append("<li>");
				stringBuilder.append("<a class='active' href='javascript:doPage(1)'>1</a>");
				stringBuilder.append("</li>");
			}
			if(pageInfo.getIndex()+1 > pageInfo.getPages()){
				stringBuilder.append("<li><a href='javascript:doPage(").append(pageInfo.getPages()).append(")'").append(" id='next'>").append("下一页</a></li>");
			}else{
				stringBuilder.append("<li><a href='javascript:doPage(").append(pageInfo.getIndex()+1).append(")'").append(" id='next'>").append("下一页</a></li>");
			}
			stringBuilder.append("<li><a href='javascript:doPage(").append(pageInfo.getPages()).append(")'").append(" id='last'>").append("尾页</a></li>");
	    	stringBuilder.append("<form id='job_pager_jump' action='#'>");
	    	stringBuilder.append("<li class='jump'>跳到<input name='jump_page' type='text' id='jump_page' ");
			stringBuilder.append(" onkeyup=\"this.value=this.value.replace(/\\D/g,'')\" onafterpaste=\"this.value=this.value.replace(/\\D/g,'')\"");
			stringBuilder.append(" value=").append(pageInfo.getIndex()).append(" >页</li>");
			stringBuilder.append("<li><a href='javascript:void(0);' onclick='jump_page()'>确定</a></li>");
			stringBuilder.append("</form>");
			stringBuilder.append("</ul>");
		}
		stringBuilder.append("</div>");
		return stringBuilder;
	}
	/**
	 * 随机生存用户名后九位数
	 * @return
	 */
	public static String liveName(){
		String[] num = {"0","1","2","3","4","5","6","7","8","9"};
		Random random = new Random();
		String temp = "";
		for(int i=0;i<9;i++){
			int a=random.nextInt(10);
			temp += num[a];
		}
		String name = "szd-"+temp;
		return name;
	}
	
	/**
	 * 借款标申请类型
	 */
	public static Map<String, Integer> borrowApplyMap = new HashMap<String, Integer>();
	static {
		borrowApplyMap.put("car", 3);//车贷E通
		borrowApplyMap.put("mortgageCar", 4);//车商贷
		borrowApplyMap.put("house", 5);//房E贷
		borrowApplyMap.put("business", 6);//商易贷
		borrowApplyMap.put("mortgage", 7);//抵押贷
		borrowApplyMap.put("guarantee", 8);//担保贷
    }
	
	public static String number2CNMontrayUnit(BigDecimal  money) {
		String CN_FULL = "整";
		String CN_NEGATIVE = "负";
	    int MONEY_PRECISION = 2;
	   	String CN_ZEOR_FULL = "零元" + CN_FULL;
		String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆","伍", "陆", "柒", "捌", "玖" };
		String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",  "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
		StringBuffer sb = new StringBuffer();
        int signum = money.signum();
         if (signum == 0) {
             return CN_ZEOR_FULL;
         }
         long number = money.movePointRight(MONEY_PRECISION)
                .setScale(0, 4).abs().longValue();
         long scale = number % 100;
         int numUnit = 0;
         int numIndex = 0;
        boolean getZero = false;
         if (!(scale > 0)) {
             numIndex = 2;
             number = number / 100;
             getZero = true;
         }
         if ((scale > 0) && (!(scale % 10 > 0))) {
             numIndex = 1;
             number = number / 10;
             getZero = true;
         }
         int zeroSize = 0;
         while (true) {
             if (number <= 0) {
                 break;
             }
             numUnit = (int) (number % 10);
             if (numUnit > 0) {
                 if ((numIndex == 9) && (zeroSize >= 3)) {
                     sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
                 }
                 if ((numIndex == 13) && (zeroSize >= 3)) {
                     sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
                 }
                 sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                 sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                 getZero = false;
                 zeroSize = 0;
             } else {
                 ++zeroSize;
                if (!(getZero)) {
                    sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                 }
                 if (numIndex == 2) {
                    if (number > 0) {
                        sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                    }
                } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                }
                getZero = true;
            }
            number = number / 10;
            ++numIndex;
        }
        if (signum == -1) {
           sb.insert(0, CN_NEGATIVE);
        }
        if (!(scale > 0)) {
            sb.append(CN_FULL);
       }
        return sb.toString();
	}
}
